package com.wst.system.controller;

import com.alibaba.fastjson2.JSONObject;
import com.wst.system.security.IsAdmin;
import com.wst.system.server.entity.vo.PageSearchVo;
import com.wst.system.util.JSONUtil;
import com.wst.system.util.ResultUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.wst.system.config.exception.GlobalExcetion;
import java.io.Serializable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.wst.system.server.service.SysOpenPermissionService;
import com.wst.system.server.entity.SysOpenPermission;
import com.wst.system.server.entity.vo.SysOpenPermissionVo;
import org.springframework.validation.annotation.Validated;
import java.util.List;
import java.util.Map;

import com.wst.system.config.PageData;


/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author X.I.O
 * @since 2025-01-16
 */

@RestController
@Api(tags = {"的服务类"})
@RequestMapping("/sys-open-permission")
public class SysOpenPermissionController  {

        @Autowired
        private SysOpenPermissionService sysOpenPermissionService;


        @IsAdmin
        @PostMapping("/saveorupdate")
        @ApiOperation(value = "的新增/更新接口",notes = "的新增接口")
        public JSONObject save(@RequestBody @Validated SysOpenPermissionVo sysOpenPermissionVo) throws GlobalExcetion{
            sysOpenPermissionService.saveOrUpdateSysOpenPermission(sysOpenPermissionVo);
            return ResultUtil.successInfo("操作成功");
        }



        @IsAdmin
        @DeleteMapping("/delbyid")
        @ApiOperation(value = "的删除接口",notes = "的删除接口")
        public JSONObject delete(@RequestParam("id") Serializable id) throws GlobalExcetion{
            boolean flag = sysOpenPermissionService.removeById(id);
            if(flag){
                return ResultUtil.successInfo("删除成功");
            }else {
                return ResultUtil.errorInfo("删除失败");
            }
        }

        @IsAdmin
        @PostMapping("/del/batch")
        @ApiOperation(value = "的批量删除接口",notes = "的批量删除接口")
        public JSONObject deleteBatch(@RequestBody List<Integer> ids) throws GlobalExcetion {
            boolean flag =  sysOpenPermissionService.removeByIds(ids);
            if(flag){
                return ResultUtil.successInfo("删除成功");
            }else {
                return ResultUtil.errorInfo("删除失败");
            }
        }

        @GetMapping("/getall")
        @ApiOperation(value = "的获取全部接口",notes = "的获取全部接口")
        public JSONObject findAll() throws GlobalExcetion {
            List<SysOpenPermission> list= sysOpenPermissionService.list();
            return ResultUtil.successInfo(list);
        }

        @GetMapping("/getbyid")
        @ApiOperation(value = "的获取一个接口",notes = "的获取一个接口")
        public JSONObject findOne(@RequestParam("id") Serializable id) throws GlobalExcetion {
            SysOpenPermission data= sysOpenPermissionService.getById(id);
            return ResultUtil.successInfo(data);
        }

        @PostMapping("/page")
        @ApiOperation(value = "的分页接口",notes = "的分页接口")
        public JSONObject findPage(@RequestBody PageSearchVo vo) throws GlobalExcetion {
            if(!JSONUtil.isNotBlank(vo.getSearch(),"appId")){
                throw new GlobalExcetion(1,"appId不得为空");
            }
            return sysOpenPermissionService.page(vo);
        }
}

